import { View } from "@tarojs/components";
import { useState, useEffect } from "react";
import ChatTaskItem from "../ChatTaskItem";
import { InfiniteLoading } from "@nutui/nutui-react-taro";
import "./index.scss";
import Taro from "@tarojs/taro";
import ScrollFooter from "../ScrollFooter";
import SupEmpty from "../SupEmpty";
import { getTaskList } from "./service";

interface ChatTaskType {
  id: string;
  name: string;
  scheduleConf: string;
  scheduleType: 0 | 1;
  status: 0 | 1;
  formatScheduleConf: string;
  updateTime: string;
  createTime: string;
  conversationResp?: {
    answer: string;
    data: any[];
  };
}

const defaultTask: ChatTaskType[] = [
  {
    id: "1",
    name: "生成最近三年的运营数据柱状图",
    scheduleConf: "生成最近三年的运营数据柱状图",
    formatScheduleConf: "每三天",
    scheduleType: 0,
    status: 1,
    updateTime: "2023.12.08",
    createTime: "2023.12.08",
  },
  {
    id: "2",
    name: "生成最近三年的运营数据柱状图",
    scheduleConf: "生成最近一年的运营数据柱状图",
    formatScheduleConf: "每三天",
    scheduleType: 0,
    status: 0,
    updateTime: "2023.12.08",
    createTime: "2023.12.08",
  },
];

interface IProps {}

const ChatTask: React.FC<IProps> = () => {
  const [chatTaskList, setChatTaskList] = useState<ChatTaskType[]>(defaultTask);

  const getChatTaskList = async () => {
    const params: any = {
      data: {},
      requestBase: {
        page: "0-0",
        sort: "-createTime",
      },
    };

    const res = await getTaskList(params);
    const { data } = res || {};
    const { isSuccess, content } = data || {};
    if (isSuccess) {
      setChatTaskList(content?.list || []);
    }
  };

  useEffect(() => {
    getChatTaskList();
  }, []);

  return (
    <View className="chat_task">
      <View className="chat_task_title">{`任务驻守（${
        chatTaskList?.length || 0
      }）`}</View>
      <View className="chat_task_list">
        {/* {!!chatTaskList.length &&
          chatTaskList?.map((item: any) => {
            return <ChatTaskItem key={item.id} info={item}></ChatTaskItem>;
          })} */}
        {chatTaskList?.length > 0 ? (
          <InfiniteLoading
            target="scroll"
            pullRefresh
            hasMore={false}
            pullingText={<>松开刷新</>}
            loadMoreText={<ScrollFooter />}
            onRefresh={getChatTaskList}
          >
            {chatTaskList.map((item, index) => {
              return <ChatTaskItem key={item.id} info={item}></ChatTaskItem>;
            })}
          </InfiniteLoading>
        ) : (
          <SupEmpty title="暂无数据" />
        )}
      </View>
    </View>
  );
};

export default ChatTask;
